package org.zanpu.metadataserver.core.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.zanpu.metadataserver.api.util.TableEnum;
import org.zanpu.metadataserver.core.metadata.service.MetadataService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MD on 2016/11/4.
 */
@Controller
@RequestMapping("/metadataQuery")
public class MetadataQueryController extends ControllerUtilities {

    @Resource
    private MetadataService metadataService;

    /**
     * 获取元数据类型列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getTypeList")
    public List<Map<String, Object>> getTypeList() {
        String sql = select + TableEnum.META_TYPE.getName();
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getDataList/{typeId}",method = RequestMethod.GET, consumes = "application/text;charset=UTF-8")
    public List<Map<String, Object>> getDataList(@PathVariable int typeId) {
        String sql = select + TableEnum.META_DATA.getName() + " WHERE type_id=" + typeId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getDictionaryList")
    public List<Map<String, Object>> getDictionaryList() {
        String sql = select + TableEnum.META_DATA_DICTIONARY.getName();
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getDetailedList/{modelId}")
    public List<Map<String, Object>> getDetailedList(@PathVariable int modelId) {
        String sql = select + TableEnum.META_DETAILED.getName() + " WHERE model_id=" + modelId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getDColumnList/{detailedId}")
    public List<Map<String, Object>> getDColumnList(@PathVariable int detailedId) {
        String sql = select + TableEnum.META_DETAILED_COLUMN.getName() + " WHERE detailed_id=" + detailedId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getFilterList/{modelId}")
    public List<Map<String, Object>> getfilterList(@PathVariable int modelId) {
        String sql = select + TableEnum.META_FILTER.getName() + " WHERE model_id=" + modelId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getList/{modelId}")
    public List<Map<String, Object>> getList(@PathVariable int modelId) {
        String sql = select + TableEnum.META_LIST.getName() + " WHERE model_id=" + modelId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getMainList/{modelId}")
    public List<Map<String, Object>> getMainList(@PathVariable int modelId) {
        String sql = select + TableEnum.META_MAIN.getName() + " WHERE model_id=" + modelId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getModelList/{dataId}")
    public List<Map<String, Object>> getModelList(@PathVariable int dataId) {
        String sql = select + TableEnum.META_MODEL.getName() + " WHERE data_id=" + dataId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getSourceList/{dataId}")
    public List<Map<String, Object>> getSourceList(@PathVariable int dataId) {
        String sql = select + TableEnum.META_SOURCE.getName() + " WHERE data_id=" + dataId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getSColumnList/{sourceId}")
    public List<Map<String, Object>> getSColumnList(@PathVariable int sourceId) {
        String sql = select + TableEnum.META_SOURCE_COLUMN.getName() + " WHERE source_id=" + sourceId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getBaseList")
    public List<Map<String, Object>> getBaseList() {
        String sql = select + TableEnum.META_DATA_BASE.getName();
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getTableList/{baseId}")
    public List<Map<String, Object>> getTableList(@PathVariable int baseId) {
        String sql = select + TableEnum.META_DATA_TABLE.getName() + " WHERE base_id="+baseId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }

    @ResponseBody
    @RequestMapping(value = "getDictionaryList/{tableId}")
    public List<Map<String, Object>> getDictionaryList(@PathVariable int tableId) {
        String sql = select + TableEnum.META_DATA_DICTIONARY.getName() + " WHERE table_id="+tableId;
        List<Map<String, Object>> mapList = metadataService.query(sql);
        return mapList;
    }
}
